package club.babyf.nativeai.demo.algorithm;

/**
 * AddStrings.
 *
 * @author zhanglei.
 * @date 2025/4/12 10:59.
 * @description 字符串相加.
 */
public class _3_AddStrings {

    private String addStrings(String num1, String num2) {
        StringBuilder res = new StringBuilder();

        int carry = 0;
        int i = num1.length() - 1;
        int j = num2.length() - 1;

        while (i >= 0 || j >= 0 || carry != 0) {
            if (i >= 0) {
                carry += num1.charAt(i--) - '0';
            }

            if (j >= 0) {
                carry += num2.charAt(j--) - '0';
            }

            res.append(carry % 10);

            carry /= 10;
        }

        return res.reverse().toString();
    }

}
